﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ZLCMS.Business;
using NHibernate;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using Newtonsoft.Json;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Commands.User.Commands.ProductSku;
using DotNet.Common.Command;
using ZLCMS.Commands.User.Util;

namespace ZLCMS.Web.SystemManage.Basics.ProductSKU
{
    public partial class ProductList : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        public string _name;
        public string _productNo;
        public string _productId;
        public string _status;
        public string _productKind;
        public string _userAccount;

        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化全局变量值
            _name = Request["name"] == null ? "" : Request["name"];
            _productNo = Request["pno"] == null ? "" : Request["pno"];
            _productId = Request["pid"] == null ? "" : Request["pid"];
            _status = Request["status"] == null ? "" : Request["status"];
            _productKind = Request["productkind"] == null ? "" : Request["productkind"];
            _userAccount = UserKit.GetUserAccountFailLogin();

            //初始化DWZ分页控件
            _framePage = new FrameHelper(this) { };

            //初始化控件值
            tb_search_name.Text = _name;
            tb_search_productno.Text = _productNo;

            if (!IsPostBack)
            {
                string opt = Request["opt"];
                //判断操作类型
                if (opt != null)
                {
                    ShowAndDropCallBack(opt);
                }
            }

            //绑定产品状态下拉框
            ParamKit.Bind("ProductStatus", ddl_status, null, new System.Collections.Generic.List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, "-1", "-1");
            ParamKit.Bind("ProductKind", ddl_product_kind, null, new System.Collections.Generic.List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, "-1", "-1");

            ddl_status.SelectedValue = _status;
            ddl_product_kind.SelectedValue = _productKind;

            //填充网格数据
            BindList(_framePage.PageIndex, _framePage.PageSize);

        }

        #region 产品显示与下架操作 + private void ShowAndDropCallBack()
        /// <summary>
        /// 产品显示操作
        /// </summary>
        private void ShowAndDropCallBack(string opt)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICommandResult<SetProductStatusResult> res = new SetProductStatusCommand().Execute(new SetProductStatusParameter()
              {
                  Opt = opt,
                  ProductId = _productId
              });
                if (res.Error)
                {
                    _framePage.Alert(new ReturnCode()
                    {
                        statusCode = 300,
                        message = "操作失败!" + res.ErrorMsg,
                        navTabId = _framePage.NavTabId
                    });
                }
                else
                {
                    _framePage.Alert(new ReturnCode()
                   {
                       statusCode = 200,
                       message = "操作成功!",
                       navTabId = _framePage.NavTabId
                   });
                }
            }
        }
        #endregion

        #region 填充网格数据 + protected void BindList(int pageIndex, int pageSize)
        /// <summary>
        /// 填充网格数据
        /// </summary>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        protected void BindList(int pageIndex, int pageSize)
        {
            string departmentCode = "";
            if (AuthUtil.CheckAuthorization("PRODUCT_MAN", "VIEW_ALL", _userAccount))
            {
            }
            else if (AuthUtil.CheckAuthorization("PRODUCT_MAN", "VIEW_OWNER", _userAccount))
            {
                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    departmentCode = session.CreateSQLQuery("select dept_code from sys_user_info where user_account=:acc")
                        .SetParameter("acc", _userAccount).UniqueResult<string>();
                }
            }
            else
            {
                //没有权限访问
                departmentCode = "no_dept";
            }

            LoadSkuProductResult res = new LoadSkuProductCommand().Execute(new LoadSkuProductParameter()
            {
                ProductName = _name,
                ProductNo = _productNo,
                Status = _status,
                Kind = _productKind,
                OwnerAccount = _userAccount,
                DeptCode = departmentCode,
                OrderBy = "ProductNo",
                PageIndex = pageIndex,
                PageSize = pageSize
            }).Result;

            grid.DataSource = res.List;
            grid.DataBind();
            _framePage.RecordCount = res.RecordCount;
        }
        #endregion

        #region 显示操作项目链接 -protected string ShowLink(string ids, string status)
        /// <summary>
        /// 显示操作项目链接
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        protected string ShowLink(string pid, string status)
        {
            string retString = null;

            if (AuthUtil.CheckAuthorization("PRODUCT_MAN", "SJXJ", _userAccount))
            {
                if (status == "1")
                {
                    retString = "<a href='/SystemManage/Basics/ProductSKU/ProductList.aspx?pid=" + pid + "&opt=show' target='ajaxTodo' title='请确定要设置下架'>设置下架</a>&nbsp;&nbsp;";
                }
                else
                {
                    retString = "<a href='/SystemManage/Basics/ProductSKU/ProductList.aspx?pid=" + pid + "&opt=drop' target='ajaxTodo' title='请确定要设置上架'>设置上架</a>&nbsp;&nbsp;";
                }
            }
           
            return retString;
        }
        #endregion

        //protected long GetSkuProductCount(object no)
        //{
        //    using (var session = SessionFactoryHelper.CreateSession())
        //    {
        //        var result = session.CreateQuery("select count(*) from ShopSkuProduct where ProductNo='" + no.ToString() + "'").UniqueResult();
        //        return result == null ? 0 : (long)result;
        //    }
        //}

        protected string GetPicture(string path)
        {
            return path + ".thumb" + System.IO.Path.GetExtension(path);
        }
    }
}
